package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderDetailVO;
import com.sky.vo.OrderStatisticsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController("adminOrderController")
@RequestMapping("/admin/order")
@Slf4j
@Api(tags = "商家订单相关接口")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @ApiOperation("各个状态的订单数量统计")
    @GetMapping("/statistics")
    public Result OrderStatistics(){
        OrderStatisticsVO vo = orderService.getStatistics();
        log.info("各个状态的订单数量统计{}", vo);
        return Result.success(vo);
    }

    @ApiOperation("查询订单详情")
    @GetMapping("/details/{id}")
    public Result OrderDetail(@PathVariable Integer id){
        //查询订单详情
        OrderDetailVO orders = orderService.getOrderDetailById(String.valueOf(id));
        log.info("查询订单详情：{}",orders);
        return Result.success(orders);
    }

    @ApiOperation("分页查询&订单搜索")
    @GetMapping("/conditionSearch")
    public Result<PageResult> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO){
        log.info("菜品分页查询：{}",ordersPageQueryDTO);
        PageResult pageResult = orderService.getPageOrder(ordersPageQueryDTO);
        log.info(pageResult.toString());
        return Result.success(pageResult);
    }

    @ApiOperation("接单")
    @PutMapping("/confirm")
    public Result confirm(@RequestBody OrdersConfirmDTO ordersConfirmDTO){
        log.info("接单{}",ordersConfirmDTO);
        Orders orders = new Orders();
        orders.setId(ordersConfirmDTO.getId());
        orders.setStatus(Orders.CONFIRMED);
        orderService.updateStatus(orders);
        return Result.success();
    }

    @ApiOperation("拒单")
    @PutMapping("/rejection")
    public Result confirm(@RequestBody OrdersRejectionDTO ordersRejectionDTO){
        log.info("拒单{}",ordersRejectionDTO);
        OrderDetailVO orderDetail = orderService.getOrderDetailById(String.valueOf(ordersRejectionDTO.getId()));
        Orders orders = new Orders();
        orders.setId(ordersRejectionDTO.getId());
        orders.setStatus(Orders.CANCELLED);//订单状态是已取消
        if(orderDetail.getPayStatus().equals(Orders.PAID)){//如果用户已经支付，需要退款
            orders.setPayStatus(Orders.REFUND);//支付状态为退款
        }
        orders.setRejectionReason(ordersRejectionDTO.getRejectionReason());
        orderService.updateStatus(orders);
        return Result.success();
    }

    @ApiOperation("取消订单")
    @PutMapping("/cancel")
    public Result cancel(@RequestBody OrdersCancelDTO ordersCancelDTO){
        log.info("取消订单{}",ordersCancelDTO);

        OrderDetailVO orderDetail = orderService.getOrderDetailById(String.valueOf(ordersCancelDTO.getId()));
        Orders orders = new Orders();
        orders.setId(ordersCancelDTO.getId());
        orders.setStatus(Orders.CANCELLED);//状态为取消状态
        if(orderDetail.getPayStatus().equals(Orders.PAID)){//如果用户已经支付，需要退款
            orders.setPayStatus(Orders.REFUND);//支付状态为退款
        }
        orders.setCancelReason(ordersCancelDTO.getCancelReason());
        orders.setCancelTime(LocalDateTime.now());
        orderService.updateStatus(orders);
        return Result.success();
    }

    @ApiOperation("派送订单")
    @PutMapping("delivery/{id}")
    public Result delivery(@PathVariable String id){
        log.info("派送订单:{}",id);
        OrderDetailVO orderDetail = orderService.getOrderDetailById(id);
        if(orderDetail.getStatus().equals(Orders.CONFIRMED)){//接单后才能派送订单
            Orders orders = new Orders();
            orders.setId(Long.valueOf(id));
            orders.setStatus(Orders.DELIVERY_IN_PROGRESS);
            orderService.updateStatus(orders);
        }
        return Result.success();
    }
    @ApiOperation("订单送达")
    @PutMapping("/complete/{id}")
    public Result complete(@PathVariable String id){
        log.info("订单送达:{}",id);

        OrderDetailVO orderDetail = orderService.getOrderDetailById(id);
        if(orderDetail.getStatus().equals(Orders.DELIVERY_IN_PROGRESS)){//派送中才能订单送达
            Orders orders = new Orders();
            orders.setId(Long.valueOf(id));
            orders.setStatus(Orders.COMPLETED);
            orders.setDeliveryTime(LocalDateTime.now());
            orderService.updateStatus(orders);
        }
        return Result.success();
    }

}
